package com.hxbd.test.utils;

import org.testng.Assert;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hxbd.service.JsonArrayCompareService;
import com.hxbd.test.Beans.TResult;

public class JsonObjectCompare implements JsonArrayCompareService{
	private JSONObject o;
	private TResult tresult;
//	public JsonObjectCompare () {
//		
//	}
	public JsonObjectCompare (String json) {
		o = JSON.parseObject(json);
		tresult = new TResult();
	}
	
	
	
	//= != < > <= >= contains() notContains() startWith endWith notNull isNull between notBetween in notIn
	
	/**
	 * 返回reslut包含某字符串
	 * @param result
	 * @return
	 */
	public Boolean containsRs(String result) {
		return o.toString().contains(result);
	}
	
	/**
	 * 包含某字符串
	 * 示例：contains("truckno":"川A12345")
	 * @param key
	 * @param value
	 * @return Boolean
	 */
	public TResult contains (String str) {
		Boolean tmp = true;
		tmp = o.toString().contains(str);
		tresult.setResult(tmp);
		return tresult;
	}
	/**
	 * key包含某值
	 * 示例：contains(truckno,123)
	 * @param key
	 * @param value
	 * @return Boolean
	 */
	public Boolean contains (String key, String value) {
		if (!o.containsKey(key)) {
			 Assert.fail("返回数据:"+o.toString()+"不包含参数:"+key);
		}
		return o.getString(key).contains(value);
	}
	
	/**
	 * key不包含某值
	 * 示例：notContains(mileage,123)
	 * @param key
	 * @param value
	 * @return Boolean
	 */
	public Boolean notContains (String key, String value) {
		if (!o.containsKey(key)) {
			 Assert.fail("返回数据:"+o.toString()+"不包含参数:"+key);
		}
		return !o.getString(key).contains(value);
	}
	/**
	 * 包含key
	 * 示例：containsKey(mileage)
	 * @param key
	 * @return Boolean
	 */
	public Boolean containsKey (String key) {
		return o.containsKey(key);
	}
	/**
	 * 不包含key
	 * 示例：notContainsKey(mileage)
	 * @param key
	 * @return Boolean
	 */
	public Boolean notContainsKey (String key) {
		return !containsKey(key);
	}
	/**
	 * key值为空
	 * 示例：isNull(mileage)
	 * @param key
	 * @param value
	 * @return Boolean
	 */
	public Boolean isNull (String key) {
		if (!o.containsKey(key)) {
			 Assert.fail("返回数据:"+o.toString()+"不包含参数:"+key);
		}
		return InterfaceUtils.isStrEmpty(o.getString(key));
	}
	/**
	 * key值非空
	 * 示例：notNull(mileage)
	 * @param key
	 * @param value
	 * @return Boolean
	 */
	public Boolean notNull (String key) {
		if (!o.containsKey(key)) {
			 Assert.fail("返回数据:"+o.toString()+"不包含参数:"+key);
		}
		return !InterfaceUtils.isStrEmpty(o.getString(key));
	}
	/**
	 * key等于value
	 * 示例：equals(mileage,123)
	 * @param key
	 * @param value
	 * @return Boolean
	 */
	public Boolean equals (String key, String value) {
		if (!o.containsKey(key)) {
			 Assert.fail("返回数据:"+o.toString()+"不包含参数:"+key);
		}
		return o.getString(key).equals(value);
	}
	/**
	 * key等于value（忽略大小写）
	 * 示例：equalsIgnorecase(mileage,123)
	 * @param key
	 * @param value
	 * @return Boolean
	 */
	public Boolean equalsIgnorecase (String key, String value) {
		if (!o.containsKey(key)) {
			 Assert.fail("返回数据:"+o.toString()+"不包含参数:"+key);
		}
		return o.getString(key).equalsIgnoreCase(value);
	}
	/**
	 * 不等于
	 * 示例：notEquals(mileage,123)
	 * @param key
	 * @param value
	 * @return Boolean
	 */
	public Boolean notEquals (String key, String value) {
		if (!o.containsKey(key)) {
			 Assert.fail("返回数据:"+o.toString()+"不包含参数:"+key);
		}
		return !o.getString(key).equals(value);
	}
	/**
	 * key值小于
	 * 示例：1:lessThan(time,2015-01-01 00:00:00)或lessThan(time,2015-01-01)或lessThan(time,2015-01-01 00:00)
	 * 2.lessThan(mileage,1234)
	 * @param key
	 * @param value
	 * @return Boolean
	 */
	public Boolean lessThan (String key, String value) {
		if (!o.containsKey(key)) {
			 Assert.fail("返回数据:"+o.toString()+"不包含参数:"+key);
		}
		return ObjectCompare.lessThan(o.getString(key), value);
	}
	/**
	 * key值大于
	 *  示例：1:greaterThan(time,2015-01-01 00:00:00)或greaterThan(time,2015-01-01)或greaterThan(time,2015-01-01 00:00)
	 * 2.greaterThan(mileage,1234)
	 * @param key
	 * @param value
	 * @return Boolean
	 */
	public Boolean greaterThan (String key, String value) {
		if (!o.containsKey(key)) {
			 Assert.fail("返回数据:"+o.toString()+"不包含参数:"+key);
		}
		return ObjectCompare.greaterThan(o.getString(key), value);
	}
	/**
	 * key大于等于xxx
	 * 示例：greaterEquals(mileage,123)
	 * @param key
	 * @param value
	 * @return Boolean
	 */
	public Boolean greaterEquals (String key, String value) {
		return greaterThan(key, value) || equals(key, value);
	}
	/**
	 * key小于等于
	 * 示例：lessEquals(mileage,123)
	 * @param key
	 * @param value
	 * @return Boolean
	 */
	public Boolean lessEquals (String key, String value) {
		return lessThan(key, value) || equals(key, value);
	}
	
	/**
	 * 介于value1、value2之间
	 * 示例：between(mileage,123,234)或between(time,2015-01-01,2016-01-01)
	 * @param key
	 * @param value1 比这个值大于或等于
	 * @param value2 比这个值小于或等于
	 * @return Boolean
	 */
	public Boolean between (String key, String value1, String value2) {
		return greaterEquals(key, value1) && lessEquals(key, value2);
	}
	/**
	 * 不介于value1、value2之间
	 * 示例：notBetween(mileage,123,234)或notBetween(time,2015-01-01,2016-01-01)
	 * @param key
	 * @param value1 比这个值大
	 * @param value2 比这个值小
	 * @return Boolean
	 */
	public Boolean notBetween (String key, String value1, String value2) {
		return !between(key, value1, value2);
	}
	/**
	 * key值属于某些值中的一个
	 * 示例:in(mileage,"1,2,3")
	 * @param key
	 * @param value
	 * @return Boolean
	 */
	public Boolean in (String key, String value) {
		String valueArr[] = value.split(",");
		Boolean bol = false;
		for (String str : valueArr) {
			bol = bol || equals(key, str);
		}
		return bol;
	}
	/**
	 * 不属于范围内值中的任何一个
	 * 示例:notIn(mileage,"1,2,3")
	 * @param key
	 * @param value
	 * @return Boolean
	 */
	public Boolean notIn (String key, String value) {
		return !in(key, value);
	}
	/**
	 * key以XXX开头
	 * 示例:startWith(truckno,adc)
	 * @param key
	 * @param value
	 * @return Boolean
	 */
	public Boolean startWith (String key, String value) {
		if (!o.containsKey(key)) {
			 Assert.fail("返回数据:"+o.toString()+"不包含参数:"+key);
		}
		return o.getString(key).startsWith(value);
	}
	/**
	 * key以XXX结尾
	 * 示例:endWith(truckno,adc)
	 * @param key
	 * @param value
	 * @return Boolean
	 */
	public Boolean endWith (String key, String value) {
		if (!o.containsKey(key)) {
			 Assert.fail("返回数据:"+o.toString()+"不包含参数:"+key);
		}
		return o.getString(key).endsWith(value);
	}
	
}
